#include "RulesDialog.h"
#include <QColorDialog>
#include <QFontDialog>
#include "Highlighter.h"
#include "common.h"
#include <QGridLayout>

//-------------------------------------

RulesDialog::RulesDialog(QWidget *parent) :
    QDialog( parent ),
    highlighter(0)
{
    resize( 600, 400 );
    buttonBox = new QDialogButtonBox( this );
    buttonBox->setOrientation( Qt::Horizontal );
    buttonBox->setStandardButtons( QDialogButtonBox::Cancel | QDialogButtonBox::Ok );
    groupBox = new QGroupBox( this );
    btnFont = new QPushButton( "Font", groupBox );
    btnColor = new QPushButton( "Color", groupBox);
    teKeyWords = new QTextEdit( groupBox );
    teKeyWords->setLineWrapMode( QTextEdit::NoWrap );

    currFormat.setFontFamily( DEFAULT_FONT_FAMILY );
    currFormat.setFontPointSize( DEFAULT_FONT_SIZE );
    currFormat.setForeground( DEFAULT_COLOR );

    connect( buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect( buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    connect( btnFont, SIGNAL( clicked() ), this, SLOT( sltFont() ) );
    connect( btnColor, SIGNAL( clicked() ), this, SLOT( sltColor() ));

    QGridLayout *gb_layout = new QGridLayout;
    gb_layout->addWidget( teKeyWords, 0, 0, 1, 2 );
    gb_layout->addWidget( btnFont, 1, 0 );
    gb_layout->addWidget( btnColor, 1, 1 );
    groupBox->setLayout( gb_layout );

    QGridLayout *layout = new QGridLayout;
    layout->addWidget( groupBox, 0, 0 );
    layout->addWidget( buttonBox, 1, 0 );
    setLayout( layout );
}

//-------------------------------------

RulesDialog::~RulesDialog()
{
}

//-------------------------------------

void RulesDialog::sltColor()
{
    QColor c = QColorDialog::getColor( currFormat.foreground().color() );
    if( c.isValid() )
    {
        currFormat.setForeground( c );
        teKeyWords->setCurrentCharFormat( currFormat );
    }
}

//-------------------------------------

void RulesDialog::sltFont()
{
    bool ok;
    QFont f;
    f.setFamily( /*DEFAULT_FONT_FAMILY*/ currFormat.font().family() );
    f.setPointSize( /*DEFAULT_FONT_SIZE*/currFormat.font().pointSize() );
    f= QFontDialog::getFont( &ok, f );
    if( ok )
    {
        currFormat.setFont( f );
        teKeyWords->setCurrentCharFormat( currFormat );
    }
}

//-------------------------------------

void RulesDialog::accept()
{
    if( highlighter )
        highlighter->updateHighlighter( teKeyWords->document() );
    QDialog::accept();
}

//-------------------------------------

void RulesDialog::setupHighlighter( Highlighter * h )
{
    if( h == NULL )
        return;

    highlighter = h;

    teKeyWords->clear();
    QVector< HRule > * rules = highlighter->getRules();
    QString pattern;
    QString res = "";
    for( int i=0; i < rules->count(); i++ )
    {
        teKeyWords->setCurrentCharFormat( rules->at(i).format );
        pattern = rules->at(i).pattern.pattern();

        res = highlighter->convertToCStr( pattern );

        if( rules->at(i).hint.length() )
            res = rules->at(i).hint + "\t" + res;

        teKeyWords->append( res );
        res.clear();
    }


}

//-------------------------------------


























